In my iOS application I have problem in using URLSession with URLSessionConfiguration.background(withIdentifier:_). In my project i need to use background mode because my application must be able to fetch large files from server. For this case i had enabled "Background modes" - fetch and processing. Here is example of code which I use in my application (as on Your example in documentation https://developer.apple.com/documentation/foundation/url_loading_system/downloading_files_in_the_background):import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textView: UITextView!
private var downloadRequest: URLSessionDownloadTask?
private lazy var urlSession: URLSession = {
let config = URLSessionConfiguration.background(withIdentifier: "backgroundLoadingSession")
config.isDiscretionary = true
config.sessionSendsLaunchEvents = true
return URLSession(configuration: config, delegate: self, delegateQueue: nil)
}()
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
self?.fetch()
}
}
func fetch() {
guard let url = URL(string: "https://images.idgesg.net/images/idge/imported/imageapi/2019/07/26/15/cloud_istock_harnnarong-100803439-large.jpg") else { return }
downloadRequest = urlSession.downloadTask(with: url)
downloadRequest?.resume()
}
}
extension ViewController: URLSessionDownloadDelegate, URLSessionDelegate {
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
DispatchQueue.main.async { [weak self] in
self?.textView.text = downloadTask.response.debugDescription
}
}
}When I'm running this code from Xcode on iPhone everything works fine - request sent, response received. But after application was terminated (or device was restarted) on next application launch (not by Xcode) anything doesn't happen - request not sent and as result response not received. I have no idea why it doesn't works and hope for Your help. I test this code on:iPhone 6 (iOS 12.4.2), iPhone XsMax (iOS 13.3)Xcode 11.2.1 (MacOS Catalina 10.15.1)Xcode 10.3 (MacOS Mojave 10.14.6)Also, here is my Info.plist file code:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<false/>
<key>UISceneConfigurations</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
<array>
<dict>
<key>UISceneConfigurationName</key>
<string>Default Configuration</string>
<key>UISceneDelegateClassName</key>
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
<key>UISceneStoryboardFile</key>
<string>Main</string>
</dict>
</array>
</dict>
</dict>
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>processing</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>